// pages/paymenstapur/paymenstapur.js

import {setDetailPlayOut} from '../../config/https'

import Toast from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/toast/toast';
var Tool = require('../../config/tool.js');

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text:[],            //倒计时
    teams:'',           //猜你喜欢
    goods:'',
    endNum:'',          //剩余拼团人数
    orders:'',          //拼团数据
    orderLength:'',     //拼团人数
    options:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.setData({
     options
   })
  },

  showDeta(orderid, teamid){
    var that = this
    setDetailPlayOut({
      orderid: orderid,
      teamid: teamid
    }).then(res=>{

        console.log(res)
      //结束时间
      var createtime = res.goods.endtime;
      //现在的时间
      var timestamp = Date.parse(new Date()) / 1000;
      //剩余倒计时
      let last = createtime - timestamp
      clearInterval(that.time);
      //团购倒计时开始
      that.time = setInterval(() => {
        var text = Tool.count(last ? last : 0);
        that.setData({ text })
        if (last > 0) { last--; }
      }, 1000);


      let orderLength = res.orders.length
      if (res.orders.length>6){
        res.orders.length = 6
      }
      that.setData({
        goods:res.goods,
        endNum:res.n,
        orders: res.orders,
        orderLength,
        teams: res.teams
      })
    }).catch(err=>{
      Toast(err.message)
    })
  },
  //跳转团购详情
  onDetailStapur(){
    wx.navigateTo({
      url: '/pages/directbuyingto/directbuyingto?id=' + this.data.goods.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.showDeta(this.data.options.orderid, this.data.options.teamid)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that  = this
    let id = that.data.goods.id
    return {
      title: that.data.goods.title,
      path: '/pages/directbuyingto/directbuyingto?id=' + id + '&type=0' + '&userid=' + wx.getStorageSync('userInfo'), 
      imageUrl: that.data.goods.thumb,  //转发图片 默认当前页面图片
      success: (res) => {//转发成功
        console.log("转发成功", res); 
      }
    }
  }
})